#Version Checker
#Checks the version of two given files and replaces older versions with the newer one
#Works in both ways. Source and Destination have to be set in the config file.


import os
import datetime
from cfgparser import source, dest, fileType

def check(a_file):
	
	info = os.stat(a_file)
	return info.st_mtime
	
def convertToDate(a_file):
	
	return datetime.date.fromtimestamp(check(a_file))

def compare(a_file1, a_file2):
	
	version1 = check(a_file1)
	version2 = check(a_file2)
	
	if version1 > version2:
		print (a_file1, " is newer than ", a_file2)
		os.replace(a_file1, a_file2)
		print("The older file has been replaced")
	elif version1 < version2:
		print (a_file2, " is newer than ", a_file1)
		os.replace(a_file2, a_file1)
		print("The older file has been replaced")
	else:
		print ("Both files are of the same age")
		



print("last time the source was edited ", convertToDate(source()))
print("last time the destination was edited ", convertToDate(dest()))

if fileType():
	compare(source(), dest())
else:
	print("Filetypes did not match. Cant compare differing filetypes")



